package com.kzj.global.utils;

import com.kzj.domain.*;
import com.kzj.global.constant.GlobalConstant;
import com.kzj.global.exception.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Random;

@Component
public class IdGeneratorUtil {
    static StringRedisTemplate stringRedisTemplate;
    @Autowired
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate)
    {
        IdGeneratorUtil.stringRedisTemplate=stringRedisTemplate;
    }
    public static String generatorId(Class aClass)
    {
        if(aClass.equals(Student.class))
        {
            return generatorId(GlobalConstant.INCR_STUDENT_ID,GlobalConstant.STUDENT_PREFIX);
        }
        else if(aClass.equals(Teacher.class))
        {
            return generatorId(GlobalConstant.INCR_TEACHER_ID,GlobalConstant.TEACHER_PREFIX);
        }
        else if(aClass.equals(Administrator.class))
        {
            return generatorId(GlobalConstant.INCR_ADMINISTRATOR_ID,GlobalConstant.ADMINISTRATOR_PREFIX);
        }
        else if(aClass.equals(ElectiveCourse.class))
        {
            return generatorId(GlobalConstant.INCR_ELECTIVE_COURSE_ID,GlobalConstant.ELECTIVE_COURSE_PREFIX);
        }
        else if(aClass.equals(Examination.class))
        {
            return generatorId(GlobalConstant.INCR_EXAMINATION_ID,GlobalConstant.EXAMINATION_PREFIX);
        }
        else if(aClass.equals(Classroom.class))
        {
            return generatorId(GlobalConstant.INCR_CLASSROOM_ID,GlobalConstant.CLASSROOM_PREFIX);
        }
        else if(aClass.equals(CourseInformation.class))
        {
            return generatorId(GlobalConstant.INCR_COURSE_INFORMATION_ID,GlobalConstant.COURSE_INFORMATION_PREFIX);
        }
        else if(aClass.equals(StudentClass.class))
        {
            return generatorId(GlobalConstant.INCR_STUDENT_CLASS_ID,GlobalConstant.STUDENT_CLASS_PREFIX);
        }
        else if(aClass.equals(Major.class))
        {
            return generatorId(GlobalConstant.INCR_MAJOR_ID,GlobalConstant.MAJOR_PREFIX);
        }
        else if(aClass.equals(Academy.class))
        {
            return generatorId(GlobalConstant.INCR_ACADEMY_ID,GlobalConstant.ACADEMY_PREFIX);
        }
        else{
            throw new MyException(GlobalConstant.CLASS_TYPE_ERROR);
        }

    }
    public static String generatorId(String incrPrefix,String userPrefix)
    {
        int year = LocalDate.now().getYear();
        Long incr = stringRedisTemplate.opsForValue().increment(incrPrefix+year);
        String incrStr=getStringINCR(incr);
        Random random = new Random();
//        Integer randomFLag=random.nextInt(10);
        return userPrefix+(year%10000)+incrStr;
    }
    public static String getStringINCR(Long incr)
    {
        String str = incr.toString();
        if(str.length()>GlobalConstant.INCR_LEN)
            throw new MyException(GlobalConstant.OVERS_INCR);
        String prefix="";
        for(int i=0;i<(GlobalConstant.INCR_LEN-str.length());i++)
        {
            prefix+="0";
        }
        return prefix+str;
    }

}
